package com.jemmyee.core.utils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import com.jemmyee.cms.entity.ArticleCategory;
import com.jemmyee.core.entity.AdminDept;
import com.jemmyee.core.web.Constants;
import com.jemmyee.shop.entity.GoodsCategory;
import com.jemmyee.shop.entity.Region;

public class TreeXmlUtils {

	/**
	 * @Description:文章分类tree xml
	 * @author:jemmyee@gmail.com
	 * @date:2011-4-29
	 */
	public static void createArticleCategoryTreeXml(String filePath, List<ArticleCategory> cates) {
		// 使用 DocumentHelper 类创建一个文档实例。 DocumentHelper 是生成 XML 文档节点的 dom4j API
		// 工厂类。
		Document document = DocumentHelper.createDocument();
		// 使用 addElement() 方法创建根元素 root 。addElement() 用于向 XML 文档中增加元素。
		Element root = document.addElement("root");
		// root.addComment("an root");
		for (ArticleCategory cate : cates) {
			Element item = root.addElement("item");
			if (cate.getId() == 0) {
				item.addAttribute("id", "1");
			} else {
				item.addAttribute("id", cate.getId().toString());
			}
			
			if (cate.getParentId() != 0) {
				item.addAttribute("parent_id", cate.getParentId().toString());
			}
			Element content = item.addElement("content");
			Element name = content.addElement("name");
			// name.setText(" <![CDATA["+cate.getName()+"]]>");
			name.setText(cate.getName());
		}
		
		XMLWriter output;
		try {
			// 美化格式
			OutputFormat format = OutputFormat.createPrettyPrint();
			format.setEncoding(Constants.TREE_XML_FILE_ENCODING);
			output = new XMLWriter(new FileWriter(new File(filePath)), format);
			output.write(document);
			output.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	/**
	 * @Description:部门tree xml
	 * @author:jemmyee@gmail.com
	 * @date:2011-4-29
	 */
	public static void createDeptTreeXml(String filePath, List<AdminDept> depts) {
		// 使用 DocumentHelper 类创建一个文档实例。 DocumentHelper 是生成 XML 文档节点的 dom4j API
		// 工厂类。
		Document document = DocumentHelper.createDocument();
		// 使用 addElement() 方法创建根元素 root 。addElement() 用于向 XML 文档中增加元素。
		Element root = document.addElement("root");
		// root.addComment("an root");
		for (AdminDept dept : depts) {
			Element item = root.addElement("item");
			if (dept.getId() == 0) {
				item.addAttribute("id", "1");
			} else {
				item.addAttribute("id", dept.getId().toString());
			}

			if (dept.getParentId() != 0) {
				item.addAttribute("parent_id", dept.getParentId().toString());
			}
			Element content = item.addElement("content");
			Element name = content.addElement("name");
			// name.setText(" <![CDATA["+cate.getName()+"]]>");
			name.setText(dept.getName());
		}

		XMLWriter output;
		try {
			// 美化格式
			OutputFormat format = OutputFormat.createPrettyPrint();
			format.setEncoding(Constants.TREE_XML_FILE_ENCODING);
			output = new XMLWriter(new FileWriter(new File(filePath)), format);
			output.write(document);
			output.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * @Description:商品分类tree xml
	 * @author:jemmyee@gmail.com
	 * @date:2011-4-29
	 */
	public static void createGoodsCategoryTreeXml(String filePath,
			List<GoodsCategory> cates) {
		Document document = DocumentHelper.createDocument();
		Element root = document.addElement("root");
//		// 添加根节点
//		Element itemRoot = root.addElement("item");
//		itemRoot.addAttribute("id", "0");
//		Element contentRoot = itemRoot.addElement("content");
//		Element nameRoot = contentRoot.addElement("name");
//		nameRoot.setText("商品类别");
		for (GoodsCategory cate : cates) {
			Element item = root.addElement("item");
			item.addAttribute("id", cate.getId().toString());
//			if (cate.getParentId() != 0) {
				item.addAttribute("parent_id",cate.getParentId().toString());
//			}
			Element content = item.addElement("content");
			Element name = content.addElement("name");
			// name.setText(" <![CDATA["+cate.getName()+"]]>");
			name.setText(cate.getName());
		}

		XMLWriter output;
		try {
			// 美化格式
			OutputFormat format = OutputFormat.createPrettyPrint();
			format.setEncoding(Constants.TREE_XML_FILE_ENCODING);
			output = new XMLWriter(new FileWriter(new File(filePath)), format);
			output.write(document);
			output.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	
	/**
	 * @Description:区域tree xml
	 * @author:jemmyee@gmail.com
	 * @date:2011-4-29
	 */
	public static void createRegionTreeXml(String filePath,
			List<Region> objs) {
		Document document = DocumentHelper.createDocument();
		Element root = document.addElement("root");
		for (Region obj :objs) {
			Element item = root.addElement("item");
			item.addAttribute("id", obj.getId().toString());
			item.addAttribute("parent_id",obj.getParentId().toString());
			Element content = item.addElement("content");
			Element name = content.addElement("name");
			// name.setText(" <![CDATA["+cate.getName()+"]]>");
			name.setText(obj.getName());
		}
		
		XMLWriter output;
		try {
			// 美化格式
			OutputFormat format = OutputFormat.createPrettyPrint();
			format.setEncoding(Constants.TREE_XML_FILE_ENCODING);
			output = new XMLWriter(new FileWriter(new File(filePath)), format);
			output.write(document);
			output.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

}
